const app = getApp();
var pageNo = 1;
var pageSize = 10;
var url = app.data.url;
var common = require('../../utils/common.js');
import Dialog from '@vant/weapp/dialog/dialog';
import Toast from '@vant/weapp/toast/toast';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    order: [],
    selectOrderPopup: false,
    unSubscribeOrderPopup: false,
    itemInfoList: [],
    activeNames: ['1'],
    preNum: '',
    title: "",
    modelId: '4sLRplyItbyfh7G6eD87LcXqe7cidohH8vOdc7uwhQU',
    merchantId: '',
  },
  /**
     * 打电话
     */
  makeCall: function (option) {
    console.log(option);
    let item = option.currentTarget.dataset.item;
    if (!item.phone) {
      wx.showToast({
        title: "当前商家无联系方式",
        icon: "none",
        duration: 2000
      })
      return;
    }
    wx.makePhoneCall({
      phoneNumber: item.phone,
      success: function () {
        console.log("成功拨打电话")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.setNavigationBarTitle({
      title: "退订订单"
    })
    if(!options.merchantId){
      wx.showToast({
        title: "请先登录!",
        icon: "none",
        duration: 2000
      })
    }
    let _that = this;
    _that.setData({
      merchantId: options.merchantId
    })

  },

  /**
   * 修改订单状态
   */
  updateStatus: function(options) {
    let _that = this;
    let orderId = options.currentTarget.dataset.id;
    let status = options.currentTarget.dataset.status;
    if (status == '8') {
      Dialog.confirm({
        title: '温馨提醒',
        message: '拒绝接单前请先保证电联用户并告知用户, 否则被用户主动投诉会影响店铺排名哦'
      }).then(() => {
        wx.request({
          url: url + '/OrderSystemApi/updateOrderStatusByOrderId',
          data: {
            orderId: orderId,
            status: status
          },
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            if (res.data.code == 200) {
              Toast.success('操作成功!');
              _that.queryTable(pageNo, pageSize, _that.data.merchantId, false);
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: "none",
                duration: 2000
              })
            }
          },
          fail: function (res) {
            console.info('submit fail');
          },
          complete: function (res) {
            console.info('submit complete');
          }
        })
      }).catch(() => {
        // on cancel
      });
    } else {
      wx.request({
        url: url + '/OrderSystemApi/updateOrderStatusByOrderId',
        data: {
          orderId: orderId,
          status: status
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
          if (res.data.code == 200) {
            Toast.success('操作成功!');
            _that.queryTable(pageNo, pageSize, _that.data.merchantId, false);
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 2000
            })
          }
        },
        fail: function(res) {
          console.info('submit fail');
        },
        complete: function(res) {
          console.info('submit complete');
        }
      })
    }

  },

  /**
   * 查询订单
   */
  selectOrder: function(option) {
    var orderId = option.currentTarget.dataset.id;
    let queryType = option.currentTarget.dataset.querytype;
    var that = this;
    wx.navigateTo({
      url: '../../pages/pay-detail/index?orderId=' + orderId + "&queryType=" + queryType,
    })

  },
  onCloseSelect() {
    this.setData({
      selectOrderPopup: false
    });
  },
  onChange: function(event) {
    this.setData({
      activeNames: event.detail
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    pageNo = 1;
    that.queryTable(pageNo, pageSize, that.data.merchantId, false);
  },
  //列表搜索通用函数
  queryTable: function(pageNo, pageSize, merchantId, flag) {
    var that = this;
    wx.request({
      url: url + '/OrderSystemApi/listPageOrder',
      data: {
        pageNo: pageNo,
        pageSize: pageSize,
        merchantId: merchantId,
        status: 7
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        console.log(res);
        if (res.data.code == 200) {
          if (!flag) {
            that.setData({
              order: res.data.data.pageList
            })
          } else {
            if (res.data.data.pageList.length > 0) {
              that.setData({
                order: that.data.order.concat(res.data.data.pageList)
              })
            }
          }
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }

      },
      fail: function(res) {
        console.info('submit fail');
      },
      complete: function(res) {
        console.info('submit complete');
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this;
    pageNo = 1;
    that.queryTable(pageNo, pageSize, that.data.merchantId, false);
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    pageNo = pageNo + 1;
    var that = this;
    that.queryTable(pageNo, pageSize, that.data.merchantId, true);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})